package com.cencat.driver.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cencat.driver.entity.Route;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

/**
 * 路线数据访问层接口
 * 提供路线相关的数据库操作方法
 * 
 * @author cencat
 * @since 2024-01-01
 */
@Mapper
public interface RouteMapper extends BaseMapper<Route> {

    /**
     * 根据司机ID查询可用路线
     * 
     * @param driverId 司机ID
     * @param tenantId 租户ID
     * @return 路线列表
     */
    @Select("SELECT * FROM biz_route WHERE driver_id = #{driverId} AND tenant_id = #{tenantId} AND status = 1 AND deleted = 0")
    List<Route> findAvailableRoutesByDriverId(@Param("driverId") Long driverId, @Param("tenantId") Long tenantId);

    /**
     * 根据起点和终点查询路线
     * 
     * @param startLocation 起点
     * @param endLocation 终点
     * @param tenantId 租户ID
     * @return 路线列表
     */
    @Select("SELECT * FROM biz_route WHERE start_location = #{startLocation} AND end_location = #{endLocation} AND tenant_id = #{tenantId} AND status = 1 AND deleted = 0")
    List<Route> findRoutesByLocation(@Param("startLocation") String startLocation, @Param("endLocation") String endLocation, @Param("tenantId") Long tenantId);

    /**
     * 分页查询司机路线
     * 
     * @param page 分页参数
     * @param driverId 司机ID
     * @param tenantId 租户ID
     * @param status 路线状态
     * @return 分页结果
     */
    @Select("<script>" +
            "SELECT * FROM biz_route WHERE tenant_id = #{tenantId} AND deleted = 0" +
            "<if test='driverId != null'> AND driver_id = #{driverId}</if>" +
            "<if test='status != null'> AND status = #{status}</if>" +
            " ORDER BY create_time DESC" +
            "</script>")
    IPage<Route> selectRoutesByPage(Page<Route> page, @Param("driverId") Long driverId, @Param("tenantId") Long tenantId, @Param("status") Integer status);

    /**
     * 根据距离范围查询路线
     * 
     * @param minDistance 最小距离
     * @param maxDistance 最大距离
     * @param tenantId 租户ID
     * @return 路线列表
     */
    @Select("SELECT * FROM biz_route WHERE distance >= #{minDistance} AND distance <= #{maxDistance} AND tenant_id = #{tenantId} AND status = 1 AND deleted = 0")
    List<Route> findRoutesByDistanceRange(@Param("minDistance") BigDecimal minDistance, @Param("maxDistance") BigDecimal maxDistance, @Param("tenantId") Long tenantId);

    /**
     * 更新路线状态
     * 
     * @param routeId 路线ID
     * @param status 新状态
     * @param tenantId 租户ID
     * @return 更新行数
     */
    @Update("UPDATE biz_route SET status = #{status}, update_time = NOW() WHERE id = #{routeId} AND tenant_id = #{tenantId} AND deleted = 0")
    int updateRouteStatus(@Param("routeId") Long routeId, @Param("status") Integer status, @Param("tenantId") Long tenantId);

    /**
     * 统计司机路线数量
     * 
     * @param driverId 司机ID
     * @param tenantId 租户ID
     * @return 路线数量
     */
    @Select("SELECT COUNT(*) FROM biz_route WHERE driver_id = #{driverId} AND tenant_id = #{tenantId} AND deleted = 0")
    Long countRoutesByDriverId(@Param("driverId") Long driverId, @Param("tenantId") Long tenantId);

    /**
     * 查询热门路线（按使用频次排序）
     * 
     * @param tenantId 租户ID
     * @param limit 限制数量
     * @return 路线列表
     */
    @Select("SELECT * FROM biz_route WHERE tenant_id = #{tenantId} AND status = 1 AND deleted = 0 ORDER BY usage_count DESC LIMIT #{limit}")
    List<Route> findPopularRoutes(@Param("tenantId") Long tenantId, @Param("limit") Integer limit);

    /**
     * 根据路线编码查询路线
     * 
     * @param routeCode 路线编码
     * @param tenantId 租户ID
     * @return 路线信息
     */
    @Select("SELECT * FROM biz_route WHERE route_code = #{routeCode} AND tenant_id = #{tenantId} AND deleted = 0")
    Route findByRouteCode(@Param("routeCode") String routeCode, @Param("tenantId") Long tenantId);

    /**
     * 批量更新路线司机
     * 
     * @param routeIds 路线ID列表
     * @param driverId 新司机ID
     * @param tenantId 租户ID
     * @return 更新行数
     */
    @Update("<script>" +
            "UPDATE biz_route SET driver_id = #{driverId}, update_time = NOW() WHERE tenant_id = #{tenantId} AND deleted = 0 AND id IN" +
            "<foreach collection='routeIds' item='routeId' open='(' separator=',' close=')'>" +
            "#{routeId}" +
            "</foreach>" +
            "</script>")
    int batchUpdateRouteDriver(@Param("routeIds") List<Long> routeIds, @Param("driverId") Long driverId, @Param("tenantId") Long tenantId);
}